package com.example.xiefeihong.chatroom.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.xiefeihong.chatroom.R;
import com.example.xiefeihong.chatroom.dto.User;

public class MainActivity extends BaseActivity {

    private EditText textName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnLog = (Button) findViewById(R.id.btn_login);
        final SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
        String nickname = pref.getString("nickname", "");
        textName = (EditText) findViewById(R.id.text_name);
        if(nickname != null){
            textName.setText(nickname);
        }
        btnLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String nickname = textName.getText().toString();
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("nickname", nickname);
                editor.commit();
                User user = new User(nickname);
                Intent intent = new Intent(MainActivity.this, RoomActivity.class);
                intent.putExtra("user", user);
                startActivity(intent);
            }
        });
    }
}
